//
//  ScoresList.m
//  ToucheTouche
//
//  Created by polytech polytech on 12/01/12.
//  Copyright (c) 2012 polytech. All rights reserved.
//

#import "ScoresList.h"

@implementation ScoresList

@synthesize stringScoresListWorld;
@synthesize stringScoresListLocal;

static ScoresList *sharedScoresList = nil;

-(id)init {
    
    if (self = [super init]) {
        stringScoresListWorld = [[NSString alloc] init];
        stringScoresListLocal = [[NSString alloc] init];
        [self loadScoreList];
    }
    return self;
}

-(void)dealloc {
    [stringScoresListWorld release];
    [stringScoresListLocal release];
    [super dealloc ];
}

-(void)loadScoreList {
    //stringScoresListLocal = [NSString stringWithString:[device description]];
    
    NSURL *pathScoreWorld = [NSURL URLWithString:@"http://www.sacre-willy.com/touche-touche/?action=iphone"];
    NSURLRequest *request = [NSURLRequest requestWithURL:pathScoreWorld];
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    
    stringScoresListWorld = [[[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

    NSString *stringUrl = [[NSString stringWithFormat:@"http://www.sacre-willy.com/touche-touche/?action=iphone&phone_id=%@",
                            [[UIDevice currentDevice] uniqueIdentifier]]stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
    NSURL *pathScoreLocal = [NSURL URLWithString:stringUrl];
    request = [NSURLRequest requestWithURL:pathScoreLocal];
    response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    stringScoresListLocal = [[[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}

////////////////////
// Singleton Methods
+ (ScoresList *)sharedScoresList
{
    if (sharedScoresList == nil) {
        sharedScoresList = [[super allocWithZone:NULL] init];
    }
    return sharedScoresList;
}

+ (id)allocWithZone:(NSZone *)zone {
    return [[self sharedScoresList] retain];
}

- (id)copyWithZone:(NSZone *)zone {
    return self;
}

- (id)retain {
    return self;
}

- (NSUInteger)retainCount {
    return NSUIntegerMax;  //denotes an object that cannot be released
}

- (void)release {
    //do nothing
}

- (id)autorelease {
    return self;
}

////////////////////
// Shared Methods
- (NSString *)getStringScoresListWorld {
    NSLog(@"stringScoresListWorld=%@", stringScoresListWorld);
    return stringScoresListWorld;  
}

- (NSString *)getStringScoresListLocal {
    NSLog(@"stringScoresListLocal=%@", stringScoresListLocal);
    return stringScoresListLocal; 
}

- (BOOL)add:(NSNumber *)score nickname:(NSString *)nickname {    
    NSString *stringUrl = [[NSString stringWithFormat:@"http://www.sacre-willy.com/touche-touche/?action=add&score=%@&nickname=%@&phone_id=%@&version=%@&os=%@&model=%@&device_name=%@",
                          score,
                          nickname,
                          [[UIDevice currentDevice] uniqueIdentifier],
                          [[UIDevice currentDevice] systemVersion],
                          [[UIDevice currentDevice] systemName],
                          [[UIDevice currentDevice] model],
                          [[UIDevice currentDevice] name]]stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
    NSURL *pathScoreAdd = [NSURL URLWithString:stringUrl];
    NSURLRequest *request = [NSURLRequest requestWithURL:pathScoreAdd];
    [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    //[self loadScoreList];
    
    return YES;
}
@end